<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\PinkValidate;
use App\library\services\PinkService;
use App\Models\PinkConfig;
use Illuminate\Http\Request;

class PinkController extends Controller
{

    /**
     * Author:Yingbo Sun
     * 团购列表
     * @param Request $request
     */
    public function pinkList(Request $request)
    {
        $province = $request->get('province',0);
        $uid = $request->uid;
        $data = PinkService::pinkList($province,$uid);
        return $this->res($data);
    }

    /**
     * Author:Yingbo Sun
     * 用户参团
     * @param Request $request
     * @return mixed
     */
    public function joinPink(Request $request)
    {
        $province = $request->get('province',0);
        $uid = $request->uid;
        $groupId = $request->post('group_id',0);
        if (empty($uid) || empty($groupId) || empty($province)){
            return $this->fail('参数错误');
        }
        $res = PinkService::joinPink($uid,$groupId,$province);
        return $this->res($res);
    }

    /**
     * Author:Yingbo Sun
     * 团购列表
     * @param Request $request
     */
    public function agentPink(Request $request)
    {
        $province = $request->get('province',0);
        $uid = $request->uid;
        if (empty($uid) || empty($province)){
            return $this->fail('参数错误');
        }
        $data = PinkService::agentPinkList($province,$uid);
        return  $this->res($data);
    }

    /**
     * Author:Yingbo Sun
     * 经纪人创建团购
     * @param Request $request
     * @return mixed
     */
    public function pinkCreate(Request $request)
    {
        $params = $request->post('data');
        $uid = $request->uid;
        if (empty($uid)){
            return $this->fail('参数错误');
        }
        $params['broker_id'] = $uid;
        $data = PinkConfig::creation($params);
        if($data){
            return  $this->success('添加成功');
        }else{
            return $this->fail('添加失败');
        }
    }

    /**
     * Author:Yingbo Sun
     * 经纪人修改团购状态
     * @param Request $request
     * @return mixed
     */
    public function pinkUpdateStatus(Request $request)
    {
        $uid = $request->uid;
        $groupId = $request->post('group_id');
        $status = $request->post('status');
        if (empty($uid) || empty($groupId)){
            return $this->fail('参数错误');
        }
        $status = $status ? 1: 0;
        $data = PinkService::agentUpdateStatus($uid,$groupId,$status);
        return  $this->res($data);
    }
}
